var Polygon = AbstractPrimitive.extend({
    init: function(points){
        this._points = points;
    },
    setPoints: function(points){
        this._points = points;
    },
    getPoints: function(){
        return this._points;
    },
    getBox: function(){
        var minX, minY, maxX, maxY = null;
        for(var i =0 ; i < this.getPoints().length ; i++){
            var point = this.getPoints()[i];
            minX = (point.getX() < minX || minX == null) ? point.getX() : minX;
            minY = (point.getY() < minY || minY == null) ? point.getY() : minY;
            maxX = (point.getX() > maxX || maxX == null) ? point.getX() : maxX;
            maxY = (point.getY() > maxY || maxY == null) ? point.getY() : maxY;
        }
        var
        topLeft = new Point(minX,minY),
        topRight = new Point(maxX,minY),
        bottomLeft = new Point(minX,maxY),
        width = topRight.getX() - topLeft.getX(),
        height = bottomLeft.getY() - topRight.getY();
        return new Rectangle(topLeft, width, height);
    },
    getCenter: function(){
        return this.getBox().getCenter();
    },
    getIntersectionFromCenterToPoint: function(point){
        return this.getBox().getIntersectionFromCenterToPoint(point);
    },
    translate: function(x, y){
        for(var i =0 ; i < this.getPoints().length ; i++){
            this.getPoints()[i].setX(this.getPoints()[i].getX() + x);
            this.getPoints()[i].setY(this.getPoints()[i].getY() + y);
        }
        return this;
    },
    rotate: function(radius,pivot){
        var pivot = (pivot != null ? pivot : new Point(0,0));
        var radius = Math.PI * radius / 180;
        for(var i =0 ; i < this.getPoints().length ; i++){
            var x = this.getPoints()[i].getX();
            var y = this.getPoints()[i].getY();
            this.getPoints()[i].setX(pivot.getX() + (x - pivot.getX()) * Math.cos(radius) - (y - pivot.getY()) * Math.sin(radius));
            this.getPoints()[i].setY(pivot.getY() + (x - pivot.getX()) * Math.sin(radius) + (y - pivot.getY()) * Math.cos(radius));
        }
        return this;
    },
    scale: function(multiplier){
        for(var i =0 ; i < this.getPoints().length ; i++){
            var point = this.getPoints()[i];
            point.setX(point.getX()*multiplier);
            point.setY(point.getY()*multiplier);
        }
    }
});